﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using QuanLyHopDong.Biz.Model;

namespace QuanLyHopDong.Biz
{
    public class DonViBiz
    {
        #region Selection

        public List<DonVi> GetAllDonVis()
        {
            using (var db = new HopDongEntities())
            {
                return db.DonVis.OrderBy(x => x.Ten).ToList();
            }
        }

        public List<DonVi> GetDonVis(int startIndex, int pageSize, string sorting, string name, out int totalRecords)
        {
            using (var db = new HopDongEntities())
            {
                var query = from item in db.DonVis
                            select item;

                // filter
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Ten.Contains(name));
                }

                totalRecords = query.Count();

                return pageSize > 0
                           ? query.OrderBy(sorting).Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public DonVi GetById(int id)
        {
            using (var db = new HopDongEntities())
            {
                return db.DonVis.FirstOrDefault(x => x.Id == id);
            }
        }

        public List<DonVi> SearchDonvis(string tenDonVi)
        {
            using (var db = new HopDongEntities())
            {
                return db.DonVis.Where(x => x.Ten.Contains(tenDonVi)).ToList();
            }
        }

        #endregion

        #region CRUD
        
        public void CreateDonVi(DonVi donVi)
        {
            using (var db = new HopDongEntities())
            {
                // NameIsExisted
                var count = (from o in db.DonVis
                             where (o.Ten == donVi.Ten)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.DonVis.Add(donVi);
                db.SaveChanges();
            }
        }

        public void UpdateDonVi(DonVi donVi)
        {
            using (var db = new HopDongEntities())
            {
                //NameIsExisted
                var count = (from o in db.DonVis
                             where (o.Ten == donVi.Ten) && (o.Id != donVi.Id)
                             select o.Id).Count();

                if (count > 0)
                    throw new ApplicationException("NameIsExisted");

                db.DonVis.Attach(donVi);
                db.Entry(donVi).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public void DeleteDonVi(int id)
        {
            if (id <= 0) return;
            using (var db = new HopDongEntities())
            {
                db.DeleteDonVi(id);
            }
        }

        #endregion
    }
}
